package com.bootdo.member.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.member.domain.ShequIntegralRecord;
import com.bootdo.member.service.ShequIntegralRecordService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 积分兑换记录
 * 
 * @author lujingqian
 * @email 2100272728@qq.com
 * @date 2019-11-08 17:24:00
 */
 
@Controller
@RequestMapping("/member/shequIntegralRecord")
public class ShequIntegralRecordController extends BaseController{


	@Autowired
	private ShequIntegralRecordService shequIntegralRecordService;
	
	@ModelAttribute
	public ShequIntegralRecord get(@RequestParam(required=false) Long id) {
		if (id != null){
			return shequIntegralRecordService.get(id);
		}else{
			return new ShequIntegralRecord();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/member/shequIntegralRecord','member:shequIntegralRecord:shequIntegralRecord',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','member:shequIntegralRecord:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("member:shequIntegralRecord:shequIntegralRecord")	
	String ShequIntegralRecord(ShequIntegralRecord shequIntegralRecord,HttpServletRequest request,HttpServletResponse response){
	    return "member/shequIntegralRecord/shequIntegralRecord";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("member:shequIntegralRecord:shequIntegralRecord")
	public PageUtils list(@RequestParam Map<String, Object> params){
		UserDO user = ShiroUtils.getUser();
		if(user.getUserType()!=1){
			params.put("communityId",user.getCommunityId());
		}
		//查询列表数据
        Query query = new Query(params);
		List<ShequIntegralRecord> shequIntegralRecordList = shequIntegralRecordService.list(query);
		for(ShequIntegralRecord record:shequIntegralRecordList){
			String remarks = record.getRemarks();
			String replace = remarks.replace("<br/>", "   ");
			record.setRemarks(replace);
		}
		int total = shequIntegralRecordService.count(query);
		PageUtils pageUtils = new PageUtils(shequIntegralRecordList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("member:shequIntegralRecord:edit")
	String add(ShequIntegralRecord shequIntegralRecord,HttpServletRequest request,HttpServletResponse response){
	    return "member/shequIntegralRecord/shequIntegralRecordedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(ShequIntegralRecord shequIntegralRecord) {
		return R.ok().put("i",shequIntegralRecord);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("member:shequIntegralRecord:edit")
	public R save( ShequIntegralRecord shequIntegralRecord){
	
		if(shequIntegralRecordService.save(shequIntegralRecord)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("member:shequIntegralRecord:edit")
	public R remove( Long id){
		if(shequIntegralRecordService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	/**
	 * 取消操作
	 */
	@PostMapping( "/cancel")
	@ResponseBody
	@RequiresPermissions("member:shequIntegralRecord:edit")
	public R cancel(Long id){
		ShequIntegralRecord record = shequIntegralRecordService.get(id);
		if(record.getState()!=0){
				return R.error("用户已完成操作，操作失败");
		}else{

			int i = shequIntegralRecordService.updateState(3, id);
			if(i>0){
				R.ok();
			}
		}
		return R.error("用户已完成操作，操作失败");
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("member:shequIntegralRecord:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		shequIntegralRecordService.batchRemove(ids);
		return R.ok();
	}
	
}